package com.ruoyi.hotel.controller;

import java.security.Security;
import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.service.OssService;
import com.ruoyi.hotel.domain.*;
import com.ruoyi.hotel.domain.bo.HmBookBo;
import com.ruoyi.hotel.domain.vo.HmBookVo;
import com.ruoyi.hotel.mapper.HmBookMapper;
import com.ruoyi.hotel.mapper.HmCheckinMapper;
import com.ruoyi.hotel.mapper.HmRoomMapper;
import com.ruoyi.hotel.mapper.HmRoomTypeMapper;
import com.ruoyi.hotel.service.IHmCheckinService;
import com.ruoyi.hotel.service.IHmRoomService;
import com.ruoyi.hotel.service.IHmRoomTypeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.service.IHmBookService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约房间操作
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/book")
public class HmBookController extends BaseController {

    private final IHmBookService iHmBookService;
    private final IHmRoomTypeService iHmRoomTypeService;
    private final IHmCheckinService iHmCheckinService;
    private final IHmRoomService iHmRoomService;


    /**
     * 查询所有预约列表
     */
    @SaCheckPermission("hotel:book:list")
    @GetMapping("/list")
    public TableDataInfo<HmBookVo> list(HmBookBo bo, PageQuery pageQuery) {
        return iHmBookService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取单个预约详细信息
     *
     * @param bookId 主键
     */
    @SaCheckPermission("hotel:book:query")
    @GetMapping("/{bookId}")
    public R<HmBookVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bookId) {
        return R.ok(iHmBookService.queryById(bookId));
    }

    /**
     * 修改预约
     */
    @SaCheckPermission("hotel:book:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmBookBo bo) {
        return toAjax(iHmBookService.updateByBo(bo));
    }


    /**
     *获得房间类型
     */
    @RequestMapping("/getRoomType")
    public TableDataInfo<HmRoomType> getRoomType(){
        return TableDataInfo.build(iHmRoomTypeService.getRoomType());
    }

    /**
     *获得房间类型
     */
    @RepeatSubmit()
    @RequestMapping("/submitBook")
    public R<Void> submitBook(@RequestBody HmBookBo book){
        Long id = getUserId();

        //查看是不是已预约或已入住
        if(iHmBookService.checkBook(id)||iHmCheckinService.checkBook(id)){
            return toAjax(false);
        }

        //查看有没有房间是空闲
        if(!iHmRoomService.checkBook(book.getRoomType())){
            return toAjax(false);
        }

        HmRoom hmRoom=iHmRoomService.book(book.getRoomType());
        book.setRoomId(hmRoom.getRoomId());
        book.setCustomerId(id);
        book.setIsExist("y");
        iHmBookService.insertByBo(book);
        //roomType里的房间可用总数roomCount在book表中插入一行后会自动计算一次，所以不需要对其更改

        return toAjax(true);

    }

    /**
     *获取有效的个人预约(任何时候生效的只有一个）
     */
    @RequestMapping("/getMyBook")
    public TableDataInfo<HmBook> getMyBook(){
        Long id=getUserId();
        return TableDataInfo.build(iHmBookService.getMyBook(id));
    }

    /**
     * 取消个人预约
     */
    @RequestMapping("/cancelMyBook")
    public void cancelMyBook(){
        Long id=getUserId();

        List<HmBook> list=iHmBookService.getMyBook(id);
        HmBook hmBook=list.get(0);

        iHmBookService.cancelBook(hmBook);
    }




}
